<template>
  <div class="add-content">
    <div class="fleetLeader">
      <div class="fleetLeader-title">车队长信息</div>
      <div class="fleetLeader-information">
        <el-form
          ref="form"
          :model="fleetLeaderOpinionForm"
          :rules="fleetLeaderOpinionRules"
          label-width="120px"
          style="margin-top: 10px;"
        >
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="手机号：" prop="mobile">
                <el-input
                  v-model="fleetLeaderOpinionForm.mobile"
                  placeholder="请输入手机号"
                  style="width: 60%;"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12" />
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="身份证正面：" prop="frontImgArray">
                <upload
                  :usage-name="'frontImgArray'"
                  @handleSucces="handleSucces"
                  @handleError="handleError"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="身份证背面：" prop="backImgArray">
                <upload
                  :usage-name="'backImgArray'"
                  @handleSucces="handleSucces"
                  @handleError="handleError"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="姓名：" prop="name">
                <el-input
                  v-model="fleetLeaderOpinionForm.name"
                  placeholder="请输入姓名"
                  style="width: 60%;"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="身份证：" prop="idCard">
                <el-input
                  v-model="fleetLeaderOpinionForm.idCard"
                  placeholder="请输入身份证号码"
                  style="width: 60%;"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="车队名称：" prop="motorcadeName">
                <el-input
                  v-model="fleetLeaderOpinionForm.motorcadeName"
                  placeholder="请输入姓名"
                  style="width: 60%;"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="服务费率：" prop="idCard">
                <el-input
                  v-model="fleetLeaderOpinionForm.taxPoint"
                  placeholder="请输入服务费率"
                  style="width: 60%;"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <span class="dialog-footer">
      <div style="margin-top:15px;display: flex;justify-content: center;">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </span>
  </div>
</template>

<script>
import { driverMasterAdd } from '@/api/tms/driverMaster'
import { fileUpload, fileDestroy } from '@/api/common/file'
import { validUserPhone } from '@/utils/validate'
import upload from '../../../components/Upload/index'

export default {
  components: {
    upload
  },
  props: {
    addVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      limitNum: 1,
      backImgEdit: false,
      frontImgEdit: false,
      frontImgIds: [],
      backImgIds: [],
      fleetLeaderOpinionForm: {
        mobile: '',
        frontImgArray: [],
        backImgArray: [],
        frontImg: '',
        backImg: '',
        name: '',
        idCard: '',
        motorcadeName: '',
        taxPoint: ''
      },
      fleetLeaderOpinionRules: {
        mobile: [{ required: true, message: '请填写手机号码', trigger: 'blur' }],
        frontImgArray: [{ required: true, message: '请上传身份证正面', trigger: 'blur' }],
        backImgArray: [{ required: true, message: '请上传身份证背面', trigger: 'blur' }],
        name: [{ required: true, message: '请填写姓名', trigger: 'blur' }],
        idCard: [{ required: true, message: '请填写身份证', trigger: 'blur' }],
        motorcadeName: [{ required: true, message: '请填写车队名称', trigger: 'blur' }],
        taxPoint: [{ required: true, message: '请填写服务费率', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleSucces(response) {
      // console.log(response)
      switch (response.usageName) {
        case 'frontImgArray':
          this.fleetLeaderOpinionForm.frontImgArray.push(response.url)
          this.fleetLeaderOpinionForm.frontImg = response.url
          break
        case 'backImgArray':
          this.fleetLeaderOpinionForm.backImgArray.push(response.url)
          this.fleetLeaderOpinionForm.backImg = response.url
          break
      }
    },
    handleError(res) {

    },
    cancel() {
      this.$router.go(-1)
    },
    add() {
      console.log(this.fleetLeaderOpinionForm)
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (!validUserPhone(this.fleetLeaderOpinionForm.mobile)) {
            this.$notify({
              title: '提醒',
              message: '请输入正确的手机号',
              type: 'error',
              duration: 2000
            })
            return
          }
          driverMasterAdd(this.fleetLeaderOpinionForm).then(() => {
            this.$notify({
              title: '成功',
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },

    exceed() {
      this.$message.error('最多只能上传1张图片')
    }
  }
}

</script>

<style>
  .add-content {
    /* border: 1px solid red; */
    margin-right: 10px;
    margin-left: 10px;
    margin-top: 20px;
  }
  .fleetLeader-title {
    font-size: 17px;
    height: 30px;
    line-height: 30px;
    width: 10%;
    text-align: center;
    border-left: 3px solid #ff9900;
  }
  .fleetLeader-information {
    border: 1px solid #e4e4e4;
    margin-top: 10px;
    padding-bottom: 10px;
  }
  .audit-opinion {
    height: 30px;
    line-height: 30px;
    background-color: #ffcc99;
    color: #ff4400;
    text-align: center;
  }
</style>
